package com.lss.fmap;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.lss.fmap.databinding.ActivityMainBinding;
import com.lss.fmap.adapter.MainViewPagerAdapter;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private ViewPager2 viewPager;

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        viewPager = binding.viewPager;
        viewPager.setAdapter(new MainViewPagerAdapter(this));

        // 底部导航点击事件
        BottomNavigationView bottomNav = binding.bottomNav;
        bottomNav.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_account) {
                viewPager.setCurrentItem(0, false);
                return true;
            } else if (itemId == R.id.navigation_task) {
                viewPager.setCurrentItem(1, false);
                return true;
            } else if (itemId == R.id.navigation_note) {
                viewPager.setCurrentItem(2, false);
                return true;
            }
            return false;
        });

        // ViewPager 滑动时更新 BottomNavigationView
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        bottomNav.setSelectedItemId(R.id.navigation_account);
                        break;
                    case 1:
                        bottomNav.setSelectedItemId(R.id.navigation_task);
                        break;
                    case 2:
                        bottomNav.setSelectedItemId(R.id.navigation_note);
                        break;
                }
            }
        });

        // 默认加载第一个页面
        viewPager.setCurrentItem(0, false);
    }
}